<div style="padding: 5px">
	<ul class="ztree" id="deptTree"></ul>
</div>
<script type="text/javascript">
	//@ sourceURL=ogarntree.js
	var organTableId = "${gridId}";
	var setting = {
		async : {
			enable : true,
			url : '${cxt!}/departmentUser/treeData',
			autoParam : [ "id=ids" ],//, "name=n", "level=lv"
			//otherParam : {"otherParam" : "zTreeAsyncTest"},
			type : "post"
		},
		data : {
			simpleData : {
				enable : true
			}
		},
		callback : {
			onCheck : organTreeOnClick,
			beforeExpand : beforeExpand,
			onExpand : onExpand,
			onAsyncSuccess : organTreeOnAsyncSuccess
		},
		check: {
			enable: true,
			chkStyle: "checkbox",
			chkboxType: { "Y": "ps", "N": "ps" }
		}
	};

	var firstAsyncSuccessFlag = 0;
	function organTreeOnAsyncSuccess(event, treeId, msg) {
		if (firstAsyncSuccessFlag == 0) {
			try {
				var deptTree = $.fn.zTree.getZTreeObj("deptTree");
				deptTree.checkAllNodes(true);
				//调用默认展开第一个结点  
				var nodes = deptTree.getNodes();
				deptTree.expandNode(nodes[0], true);
				deptTree.selectNode(nodes[0]);
				//初始化organtable
				initDeptTable(nodes[0]);

				var childNodes = organTree.transformToArray(nodes[0]);
				organTree.expandNode(childNodes[1], true);
				firstAsyncSuccessFlag = 1;
				
			} catch (err) {

			}

		}
	}

	function organTreeOnClick(event, treeId, treeNode) {
		var treeObj = $.fn.zTree.getZTreeObj("deptTree");			
		var nodes = treeObj.getCheckedNodes(true);
		var t = "";
		for(var i=0;i<nodes.length;i++){	
				t=t+nodes[i].bh+".";
		}	
		initDeptTable(t);
	}

	////////////////////下面是处理展开//////////////////

	var curExpandNode = null;
	function beforeExpand(treeId, treeNode) {
		var pNode = curExpandNode ? curExpandNode.getParentNode() : null;
		var treeNodeP = treeNode.parentTId ? treeNode.getParentNode() : null;
		var zTree = $.fn.zTree.getZTreeObj("deptTree");
		for (var i = 0, l = !treeNodeP ? 0 : treeNodeP.children.length; i < l; i++) {
			if (treeNode !== treeNodeP.children[i]) {
				zTree.expandNode(treeNodeP.children[i], false);
			}
		}
		while (pNode) {
			if (pNode === treeNode) {
				break;
			}
			pNode = pNode.getParentNode();
		}
		if (!pNode) {
			singlePath(treeNode);
		}

	}
	function singlePath(newNode) {
		if (newNode === curExpandNode)
			return;

		var zTree = $.fn.zTree.getZTreeObj("deptTree"), rootNodes, tmpRoot, tmpTId, i, j, n;

		if (!curExpandNode) {
			tmpRoot = newNode;
			while (tmpRoot) {
				tmpTId = tmpRoot.tId;
				tmpRoot = tmpRoot.getParentNode();
			}
			rootNodes = zTree.getNodes();
			for (i = 0, j = rootNodes.length; i < j; i++) {
				n = rootNodes[i];
				if (n.tId != tmpTId) {
					zTree.expandNode(n, false);
				}
			}
		} else if (curExpandNode && curExpandNode.open) {
			if (newNode.parentTId === curExpandNode.parentTId) {
				zTree.expandNode(curExpandNode, false);
			} else {
				var newParents = [];
				while (newNode) {
					newNode = newNode.getParentNode();
					if (newNode === curExpandNode) {
						newParents = null;
						break;
					} else if (newNode) {
						newParents.push(newNode);
					}
				}
				if (newParents != null) {
					var oldNode = curExpandNode;
					var oldParents = [];
					while (oldNode) {
						oldNode = oldNode.getParentNode();
						if (oldNode) {
							oldParents.push(oldNode);
						}
					}
					if (newParents.length > 0) {
						zTree.expandNode(oldParents[Math.abs(oldParents.length
								- newParents.length) - 1], false);
					} else {
						zTree.expandNode(oldParents[oldParents.length - 1],
								false);
					}
				}
			}
		}
		curExpandNode = newNode;
	}

	function onExpand(event, treeId, treeNode) {
		curExpandNode = treeNode;
	}

	//////////////////初始化////////////////////

	$(document).ready(function() {
		$.fn.zTree.init($("#deptTree"), setting);
	});
</script>